package me.winds.infrared.module.basic;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.easyder.wrapper.base.presenter.MvpBasePresenter;
import com.easyder.wrapper.base.view.StatusManagerDelegate;
import com.easyder.wrapper.base.view.WrapperStatusActivity;
import com.easyder.wrapper.core.model.BaseVo;
import com.gyf.barlibrary.ImmersionBar;
import com.luck.picture.lib.permissions.RxPermissions;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.winds.infrared.R;
import me.winds.infrared.module.basic.bean.IrBean;
import me.winds.infrared.widget.floating.FloatingActionButton;
import me.winds.infrared.widget.floating.FloatingActionsMenu;
import me.winds.widget.usage.TitleView;

/**
 * Author:  winds
 * Data:    2018/8/21
 * Version: 1.0
 * Desc:
 */


public class HomeActivity extends WrapperStatusActivity<MvpBasePresenter> {

    @BindView(R.id.iv_image)
    ImageView ivImage;
    @BindView(R.id.iv_center_temp)
    ImageView ivCenterTemp;
    @BindView(R.id.tv_max_temp)
    TextView tvMaxTemp;
    @BindView(R.id.fab_palette)
    FloatingActionButton fabPalette;
    @BindView(R.id.fab_center_temp)
    FloatingActionButton fabCenterTemp;
    @BindView(R.id.fab_camera)
    FloatingActionButton fabCamera;
    @BindView(R.id.fam_menu)
    FloatingActionsMenu famMenu;

    Disposable disposable;

    @Override
    protected int getViewLayout() {
        return R.layout.activity_home;
    }

    @Override
    protected void setImmersionBar() {
        super.setImmersionBar();
        ImmersionBar.with(this).statusBarView(R.id.status_view).statusBarDarkFont(!IrFactory.getFactory().isHasConnected()).init();
    }

    @Override
    protected void onCreateStatusManager(View view) {
        delegate = new StatusManagerDelegate(findViewById(R.id.iv_image)) {
            @Override
            public int getRequestCount() {
                return 0;
            }

            @Override
            public void load() {
                loadData(null, getIntent());
            }
        };
        delegate.getStatusManager().setClearStatusFlag(true);

        showLoadingStatus();
    }

    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {
        IrFactory.getFactory().forceInit();
        famMenu.setOnFloatingActionsMenuUpdateListener(new FloatingActionsMenu.OnFloatingActionsMenuUpdateListener() {
            @Override
            public void onMenuExpanded() {

            }

            @Override
            public void onMenuCollapsed() {

            }

            @Override
            public void onMenuStartExpand() {
                delayTimer(180);
            }
        });

    }

    @Override
    protected void loadData(Bundle savedInstanceState, Intent intent) {

    }

    @Override
    public void showContentView(String url, BaseVo dataVo) {

    }

    @Override
    protected boolean isUseTitle() {
        return false;
    }

    @Override
    protected void onStart() {
        super.onStart();
        IrFactory.getFactory().register();
        startTimer();
    }


    @Override
    protected void onStop() {
        super.onStop();
        IrFactory.getFactory().unregister();
        endTimer();
    }

    @Override
    protected void onDestroy() {
        endTimer();
        super.onDestroy();
        IrFactory.getFactory().destroy();
    }

    @Override
    public void onBackPressedSupport() {
        if (famMenu.isExpanded()) {
            famMenu.collapse();
            return;
        }
        super.onBackPressedSupport();
    }

    @OnClick({R.id.fab_palette, R.id.fab_center_temp, R.id.fab_camera})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.fab_palette:
                IrFactory.getFactory().setPalette();
                break;
            case R.id.fab_center_temp:
                if (IrFactory.getFactory().isIrSdkReady()) {
                    ivCenterTemp.setVisibility(ivCenterTemp.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
                } else {
                    showToast("红外设备未连接");
                }
                break;
            case R.id.fab_camera:
                takePicture();
                break;
        }
    }



    private void timer(long delay) {
        disposable = IrFactory.getFactory().timer(delay)
                .subscribe(new Consumer<IrBean>() {
                    @Override
                    public void accept(IrBean irBean) throws Exception {
                        if (!getStatusDelegate().getStatusManager().isHide()) {
                            getStatusDelegate().removeStatus();
                            ImmersionBar.with(mActivity).statusBarView(R.id.status_view).statusBarDarkFont(false).init();
                        }
                        if (irBean.bitmap != null) {
                            ivImage.setImageBitmap(irBean.bitmap);
                            tvMaxTemp.setText(String.format("%1$s%2$.2f%3$s", "最高温度", irBean.maxTemp, "℃\n"));
                            if (ivCenterTemp.getVisibility() == View.VISIBLE) {
                                tvMaxTemp.append(String.format("%1$s%2$.2f%3$s", "中心温度", irBean.centerTemp, "℃"));
                            }
                        }
                    }
                });
    }

    private void startTimer() {
        if (disposable == null || disposable.isDisposed()) {
            timer(0);
        }
    }

    private void endTimer() {
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
        }
    }

    private void takePicture() {
        checkStoragePermission();
    }

    /**
     * 检查权限
     */
    private void checkStoragePermission() {
        new RxPermissions(mActivity).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) {
                            delayTimer();
                            IrFactory.getFactory().takePicture(ivCenterTemp.getVisibility() == View.VISIBLE);
                        } else {
                            showToast("文件读写权限被拒绝");
                        }
                    }
                });
    }

    private void delayTimer(long delay) {
        endTimer();
        timer(delay);
    }

    private void delayTimer() {
        endTimer();
        timer(500);
    }


    private void openCamera(boolean bool){

    }

}
